package com.robot.base.configs;

import static android.content.Context.MODE_PRIVATE;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import com.alibaba.fastjson.JSON;
import com.robot.base.common.api.AbstractSpaceSubscriber;
import com.robot.base.model.LoginBean;
import com.robot.base.model.UserInfoBean;
import com.robot.base.util.LiveDataEventManager;

import api.UserServiceFactory;
import me.goldze.mvvmhabit.http.BaseResponse;

public class PrefsManager {

    protected static SharedPreferences mPreferences;

    public static SharedPreferences getMPreferences() {
        return mPreferences;
    }

    protected static SharedPreferences.Editor mEditor;

    @SuppressLint("CommitPrefEdits")
    public static void load(Context a) {
        try {
            mPreferences = a.getSharedPreferences(AppConstants.SpKey.PREFERENCE_FILE_NAME, MODE_PRIVATE);
            mEditor = mPreferences.edit();
        } catch (Exception e) {
        }
    }

    public static boolean save() {
        if (mEditor == null) {
            return false;
        }

        return mEditor.commit();
    }

    public static boolean clear() {
        if (mEditor == null) {
            return false;
        }
        mEditor.clear();
        return mEditor.commit();
    }


    public static final int LOGIN_VIA = -1;

    public static void loginSuccess() {
//        refreshUserInfo();
        LiveDataEventManager.postLoginSuccessEvent();
        if (mEditor == null) {
            return;
        }
        mEditor.putString(AppConstants.SpKey.LOGIN_STATE, AppConstants.SpKey.LOGINING);
        mEditor.commit();
    }

    public static void refreshUserInfo() {
        UserServiceFactory.userInfo().subscribe(new AbstractSpaceSubscriber<BaseResponse<UserInfoBean>>() {
            @Override
            public void onNext(BaseResponse<UserInfoBean> userInfoBeanBaseResponse) {
                saveUserInfo(userInfoBeanBaseResponse.getData());
            }
        });
    }


    public static void loginout() {
        if (mEditor == null) {
            return;
        }
        mEditor.putString(AppConstants.SpKey.LOGIN_STATE, AppConstants.SpKey.LOGINOUT);
        mEditor.clear();
        mEditor.commit();
    }


    public static String getLoginType() {

        if (mPreferences == null) {
            return "";
        }
        return mPreferences.getString(AppConstants.SpKey.LOGIN_STATE, AppConstants.SpKey.NOTLOGIN);
    }

    public static String getVideoState(String name) {
        if (mPreferences == null) {
            return "0";
        }
        String ss = mPreferences.getString(name, null);
        if (!TextUtils.isEmpty(ss)) {
            return ss;
        } else {
            return "0";
        }
    }

    public static void saveVideoState(String name, String data) {

        if (mEditor == null) {
            return;
        }
        if (null == data) {
            mEditor.putString(name, "");
        } else {
            mEditor.putString(name, data);
        }
        mEditor.commit();
    }


    public static void saveUserLoginInfo(LoginBean data) {
        if (mEditor == null) {
            return;
        }
        if (null == data) {
            mEditor.putString(AppConstants.SpKey.USERILOGININFO, "");
        } else {
            mEditor.putString(AppConstants.SpKey.USERILOGININFO, JSON.toJSONString(data));
        }
        mEditor.commit();
    }

    public static LoginBean getUserLoginInfo() {
        if (mPreferences == null) {
            return new LoginBean();
        }
        String ss = mPreferences.getString(AppConstants.SpKey.USERILOGININFO, null);
        if (!TextUtils.isEmpty(ss)) {
            return JSON.parseObject(ss, LoginBean.class);
        } else {
            return new LoginBean();
        }
    }


    public static void saveUserInfo(UserInfoBean data) {
        if (mEditor == null) {
            return;
        }
        if (null == data) {
            mEditor.putString(AppConstants.SpKey.USERINFO, "");
        } else {
            mEditor.putString(AppConstants.SpKey.USERINFO, JSON.toJSONString(data));
        }
        mEditor.apply();
        mEditor.commit();
    }


    public static UserInfoBean getUserInfo() {

        if (mPreferences == null) {
            return new UserInfoBean();
        }
        String ss = mPreferences.getString(AppConstants.SpKey.USERINFO, null);
        if (!TextUtils.isEmpty(ss)) {
            return JSON.parseObject(ss, UserInfoBean.class);
        } else {
            return new UserInfoBean();
        }
    }
}
